<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Buyorder extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Order_model', 'order');
		$this->load->model('User_model', 'users');
	}

	public function index()
	{
		if(time() < (strtotime(date("Y-m-d"))+9*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-15*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
		}else if(time() > (strtotime(date("Y-m-d"))+12*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+33*3600);
		}
	      
	       $data = array(
	          'myCreditList' => $this->order->getOneDayBuyOrder("br.time>'".$day1."' AND br.time<'".$day2."'"),
	          'bankList' => $this->users->getBankList()
	      );
		$this->load->view('buyorder', $data);
	}

	//进货订单的添加
	public function add(){
		$data = $this->input->post();
		preg_match("/^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$/",$data['price'],$m);
	    if(empty($m)){
	      jechof("非法参数");
	    }
		preg_match("/^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$/",$data['number'],$m);
	    if(empty($m)){
	      jechof("非法参数");
	    }
	    preg_match("/^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$/",$data['tax'],$m);
	    if(empty($m)){
	      jechof("非法参数");
	    }
		$data['amount'] = $data['price']*$data['number']+$data['tax'];
		$data['checkout'] == 1?$data['payment']=0:$data['payment']=1;
		$data['reviewed'] = 0;
		$data['time'] = date("Y-m-d H:i:s");
		$data['enable'] = 0;
		$data['operator'] = $_SESSION['id'];
		$data['loss'] = $data['price'] * $data['lossNum'];
		unset($data['checkout']);
		if($this->order->addBuyOrder($data)){
			jechos("添加成功");
		}else{
			jechof("添加失败");
		}
	}

	//得到一天的进货数据
	public function getOneDayData(){
		if(time() < (strtotime(date("Y-m-d"))+9*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-15*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
		}else if(time() > (strtotime(date("Y-m-d"))+12*3600)){
			$day1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+9*3600);
	     	$day2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+33*3600);
		}
		$where = "enable=0 AND time>".$day1." AND time<".$day2;
		//得到一天的所有支出
		$allTotal = $this->order->getAllBuyOrderData($where);
		$where1 = $where." AND bandid!=1";
		$buyTotal = $this->order->getAllBuyOrderData($where1);
		$where2 = $where." AND bandid=1";
		$cost = $this->order->getAllBuyOrderData($where2);
		$data['allTotal'] = $allTotal['total'];
		$data['total'] = $total['total'];
		$data['cost'] = $cost['total'];
		$data['buyWeight'] = $total['number'];
		$data['lossWeight'] = $total['lonum'];
		$data['actualWeight'] = $data['buyWeight'] - $data['lossWeight'];
		$data['lossMoney'] = $total['lo'];
		if($data){
			jechos($data);
		}else{
			jechof("获取数据失败");
		}
	}

}

/* End of file Buyorder.php */
/* Location: ./application/controllers/Buyorder.php */